//index.js
//获取应用实例
const app = getApp()
let axios = require('../../../utils/httpUtil.js')
let utils = require('../../../utils/dateUtils.js')
Page({
    data: {
        historyArr: [],
        hotArr: ["烧烤", "火锅", "麻辣烫", "米线", "鸡骨架", "烤鱼", "炒菜", "饺子", "包子", "汉堡", "面条", "快餐", "海鲜", "奶茶"],
        searchCont: null,
        queryType: 1,
        isList: false,
        focus: true,
        iptLength: 0,
        color3: 0,
        ispai: false,
        isSaix: false,
        shopList: [],
        count: 5,
        pageNum: 1,
        pageSize: 100,
        hasMoreData: true,
        shopType: null,
        paiArr: [{
                name: '综合排序',
                type: 1
            },
            {
                name: '销量最高',
                type: 11
            },
            {
                name: '起送价最低',
                type: 12
            },
            {
                name: '配送费最低',
                type: 13
            }
        ],
        saiArr: [{
                name: '满减优惠',
                type: 4,
                state: false
            },
            {
                name: '新客立减',
                type: 5,
                state: false
            },
            {
                name: '单品折扣',
                type: 6,
                state: false
            }
        ],
        paiText: '综合排序',
        pIndex: 0,
    },
    colse() {
        this.setData({
            ispai: false,
            isSaix: false,
        })
    },
    dumpShopDetail(e) {
        let id = e.currentTarget.dataset.shopid;
        wx.navigateTo({
            url: '../../shopInfo/shopInfo?shopId=' + id,
        })
    },
    getInfo() {
        let historyArr = wx.getStorageSync('historyArr');
        this.setData({
            historyArr: historyArr ? historyArr : [],
        })
    },
    searchCont: function(e) {
        if (e.detail.value.length == 0) {
            this.setData({
                iptLength: 0,
                searchCont: e.detail.value
            })
        } else {
            this.setData({
                iptLength: 1,
                searchCont: e.detail.value
            })
        }
    },
    cancelSer() {
        this.setData({
            iptLength: 0,
            searchCont: null
        })
        this.getInfo();
    },
    showHistory() {
        this.setData({
            isList: false,
            ispai: false,
            isSaix: false,
        })
        this.getInfo();
    },
    getShopLists(message) {
        wx.hideNavigationBarLoading()
        wx.showLoading({
            title: message,
        })
        let self = this;
        let para = {
            latitude: wx.getStorageSync('location').lat,
            longitude: wx.getStorageSync('location').lng,
            queryType: this.data.queryType,
            pageNum: self.data.pageNum,
            pageSize: self.data.pageSize,
        }
        if (self.data.shopType) {
            para.shopType = self.data.shopType;
        }
        if (self.data.searchCont) {
            para.shopTitle = self.data.searchCont;
        }
        axios.reqPost('xshop/getShopsList', para, res => {

            wx.hideLoading();

            if (res.resultCode == 1000 && res.returnObject) {
                let contentlistTem = self.data.shopList
                if (self.data.pageNum == 1) {
                    contentlistTem = []
                }
                let a = res.returnObject.list;
                // for(var i=0; i<10;i++){
                //   a.push(a[0])
                // }
                if (a.length < self.data.pageSize) {
                    a.map(p => {
                        if (p.distance > 1000) {
                            p.distance = (p.distance / 1000).toFixed(1) + 'km'
                        } else {
                            p.distance = p.distance + 'm'
                        }
                        p.avgStar = (p.avgStar - 0).toFixed(1);
                        //计算起送价
                        if (p.shopSendMinprice > 0) {
                            p.shopSendMinprice = (p.shopSendMinprice / 100).toFixed(1);
                        }else{
							p.shopSendMinprice = 0;
						}
                        //计算配送费
                        if (p.shopSendPrice > 0) {
                            p.shopSendPrice = (p.shopSendPrice / 100).toFixed(1);
                        }else{
							p.shopSendPrice  = 0;
						}

						let tiflag;
						var tims = p.openHours.split(",");
						for (var i = 0; i < tims.length; i++) {
							tiflag = utils.timeRange(tims[i].split("-")[0], tims[i].split("-")[1]);
							if (tiflag) {
								break;
							}
						}
						p.tiflag = tiflag;
                    })

                    self.setData({
                        shopList: contentlistTem.concat(a),
                        hasMoreData: false
                    })
                } else {
                    self.setData({
                        shopList: contentlistTem.concat(a),
                        hasMoreData: true,
                        pageNum: self.data.pageNum + 1
                    })
                }
            } else if (res.resultCode == 1000 && !res.returnObject) {
                self.setData({
                    shopList: [],
                    hasMoreData: false,
                    isList: false,
                })
            } else {
                self.setData({
                    shopList: [],
                    hasMoreData: false,
                    isList: false,
                })
                wx.showToast({
                    title: '亲，网络好像出问题了哦',
                    icon: 'none',
                    duration: 2000
                })
            }

        })
    },
    search() {
        // console.log(this.data.searchCont);
        //if (!this.data.searchCont) return;
        let historyArr = wx.getStorageSync('historyArr') ? wx.getStorageSync('historyArr') : [];
        let indexs = historyArr.indexOf(this.data.searchCont);
        if (indexs != -1) {
            historyArr.splice(indexs, 1);
            historyArr.unshift(this.data.searchCont)
        } else {
            historyArr.unshift(this.data.searchCont)
            if (historyArr.length >= 15) {
                historyArr.pop()
            }
        }
        wx.setStorageSync('historyArr', historyArr)
        this.setData({
            isList: true,
        })
        this.getShopLists('正在加载数据');
    },
    handleHotClick(e) {
        let hot = e.currentTarget.dataset.value;
        this.setData({
            searchCont: hot,
            isList: true,
            iptLength: 1
        })
        this.search();
    },
    handleHisClick(e) {
        let His = e.currentTarget.dataset.value;
        this.setData({
            searchCont: His,
            isList: true,
            iptLength: 1
        })
        this.search();
    },
    delete() {
        wx.setStorageSync('historyArr', [])
        this.setData({
            historyArr: [],
        })
    },
    handleClickXun(e) {
        console.log(e)
        let color = e.currentTarget.dataset.color;
        if (color == 0) {
            this.setData({
                color3: color,
                ispai: true,
                isSaix: false,
                queryType: 1,
                pageNum: 1,
            })
        } else if (color == 1) {
            this.setData({
                color3: color,
                ispai: false,
                queryType: 2,
                isSaix: false,
                pageNum: 1
            })
            this.getShopLists('正在加载数据');
        } else if (color == 2) {
            this.setData({
                color3: color,
                ispai: false,
                queryType: 3,
                isSaix: false,
                pageNum: 1
            })
            this.getShopLists('正在加载数据');
        } else if (color == 3) {
            this.setData({
                color3: color,
                ispai: false,
                isSaix: true,
                pageNum: 1
            })
        }
    },
    searchScrollLower() {

    },
    choosePai(e) {
        let pIndex = e.currentTarget.dataset.index;
        let paiText = e.currentTarget.dataset.paitext;
        let queryType = e.currentTarget.dataset.type;
        this.setData({
            pIndex: pIndex,
            paiText: paiText
        })
        this.setData({
            ispai: false,
            isSaix: false,
            pageNum: 1,
            queryType: queryType
        })
        this.getShopLists('加载中...');
    },
    checkboxS(e) {
        let Index = e.currentTarget.dataset.index;
        this.data.saiArr[Index].state = true;
        this.setData({
            saiArr: this.data.saiArr
        });
    },
    clearArr() {
        this.data.saiArr.map(p => {
            p.state = false;
        })
        this.setData({
            saiArr: this.data.saiArr
        })
    },
    saveArr() {
        let str = [];
        this.data.saiArr.map(p => {
            p.state ? str.push(p.type) : '';
        })
        console.log(str)
        this.setData({
            queryType: str.join(','),
            isSaix: false
        })
        this.getShopLists('加载中...');
    },
    onLoad: function(options) {
        if (options.type == 'food') {
            this.setData({
                isList: true,
                shopType: 1,
                focus: false,
            })
            wx.setNavigationBarTitle({
                title: '美食'
            })
            this.getShopLists('加载中...');
        } else if (options.type == 'shop') {
            this.setData({
                isList: true,
                shopType: 2,
                focus: false,
            })
            wx.setNavigationBarTitle({
                title: '超市'
            })
            this.getShopLists('加载中...');
        }
        this.getInfo();
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        let that = this;
        wx.stopPullDownRefresh();
        wx.showNavigationBarLoading()
        that.setData({
            pageNum: 1
        })
        that.getShopLists('加载中...');
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        console.log(123)
        if (this.data.hasMoreData) {
            this.getShopLists('加载中...');
        }
    },

})